<? include ("../init.php") ?>
<?
  $detail = $_POST['detail'];
  $img_id = $_POST['img_id'];
  
  $img_sql = '';
  
  if(isset($_FILES["gallery_img"]) && is_uploaded_file($_FILES["gallery_img"]["tmp_name"]) && $_FILES["gallery_img"]["tmp_name"]!=''){
    $image = new image();
  
    ini_set("html_errors", "0");
    
    // Check the upload
    if ($_FILES["gallery_img"]["error"] != 0) {
      echo "ERROR:invalid upload";
      exit(0);
    }
  
    $name = get_file_name($img_id, $_FILES['gallery_img']['tmp_name']);
    $path = "../../images/header_slide";
    $urlpath = get_url_path($path);
    $link = URL."/".$urlpath."/".$name;
    $filename = $path ."/". $name;
    
    if(!file_exists($path)){
      mkdir($path);
    }
  
    if (!move_uploaded_file($_FILES['gallery_img']['tmp_name'], $filename)){
      echo 'Problem: Could not move file to destination directory';
      exit(0);
    }
  
    ini_set('memory_limit', '100M');
    $image->load($filename);
    $image->resize(408,230);
    $image->save($filename);
    ini_set('memory_limit', '16M');
  
    chmod($filename,0755);
    
    $sql = "UPDATE gallery SET img_path ='$name' WHERE img_id=$img_id";
    $db->execute($sql);
    
    if(file_exists($path.'/'.$_POST['old_img']) && trim($_POST['old_img'])!=''){
      unlink($path.'/'.$_POST['old_img']);
    }
    $img_sql = ",img_path='$name'";
    //echo $img_sql;
  }
  
  $sql = "UPDATE gallery SET img_title='$detail'$img_sql WHERE img_id=$img_id";
  $db->execute($sql);
  
  header("Location:index.php");
?>